import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_utils/global.dart';

import '../../../../../hj_utils/text_util.dart';
import '../../../../../tools_base/global_store/store.dart';
import '../../../tools_base/loading/LoadingHelper.dart';

class EditNicknameLogic extends GetxController with GetTickerProviderStateMixin {
  EditNicknameLogic get to => Get.find<EditNicknameLogic>();

  TextEditingController editingController = TextEditingController();

  bool isSubmit = false;
  int type = 0; // 0修改昵称， 1：修改性别
  String? gender;
  String? name;
  int? selectIndex;

  final List<String> textList = ['男', '女', '保密'];

  @override
  void onInit() {
    super.onInit();
    type = Get.arguments["type"];
    if (type == 0) {
      name = Get.arguments["content"];
    } else {
      gender = changeGender(Get.arguments["content"] ?? "");
    }
    editingController.text = name ?? "";
  }

  generateGender(int index) {
    if (index == 0) {
      gender = "male";
    } else if (index == 1) {
      gender = "female";
    } else if (index == 2) {
      gender = "";
    }
    selectIndex = index;
    update();
  }

  changeGender(String content) {
    if (content == "男") {
      gender = "male";
      selectIndex = 0;
    } else if (content == "女") {
      selectIndex = 1;
      gender = "female";
    } else {
      selectIndex = 2;
      gender = "";
    }
    update();
  }

  void onSubmit() async {
    if (isSubmit) {
      showToast("重复提交");
      return;
    }
    isSubmit = true;
    String content = "";
    if (type == 1) {
      content = gender ?? "";
    } else {
      content = editingController.text;
    }
    if (type != 1 && TextUtil.isEmpty(content)) {
      showToast('提交内容不能为空！');
    } else {
      LoadingHelper.showLoading();
      var param = <String, dynamic>{};
      if (type == 1) {
        param['gender'] = content;
      } else {
        param['name'] = content;
      }
      final userInfo = await globalStore.updateUserInfo(map: param);
      LoadingHelper.dismissLoading();
      if (null != userInfo) {
        showToast("更新成功");
        Get.back(result: userInfo);
      }
    }
  }
}
